package com.example.demo.service.impl;

import com.example.demo.entity.FileReturn;
import com.example.demo.service.ImgService;
import com.example.demo.service.face.FileService;
import org.apache.commons.io.IOUtils;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

@Service
public class FileServiceImpl implements FileService {
    private final Logger logger = LoggerFactory.getLogger(FileServiceImpl.class);

    @Autowired
    ImgService imgService;

    @Override
    public FileReturn uploadFile(MultipartFile multipartFile, String imgid, String createtime) {
        String type = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
        //文件保存路径
//        String filePath = "G:\\bishe\\vue-学生体测系统\\filepath";
        String filePath = "D:\\vue学生体测系统\\vue学生体测系统\\filepath";
//        String filePath = "/www/wwwroot/java";
        //文件名
        String fileName = String.valueOf(System.currentTimeMillis()) + type;
        imgService.imgs(imgid, "http://localhost:8080/static/" + fileName, createtime);
//        imgService.imgs(Integer.valueOf(imgid),"http://ccc5.top:8617/static/"+fileName,createtime);
        File file = new File(filePath + File.separator + fileName);
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            IOUtils.copy(multipartFile.getInputStream(), fileOutputStream);
            System.out.println("===========file upload success=======");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //关闭
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();

            }
        }
        return new FileReturn<>(200, "文件上传成功", "http://172.16.1.101:8080/static/" + fileName);
    }
}
